package com.expert.service.Impl;

import com.expert.enums.ProjectStatus;
import com.expert.pojo.Expert;
import com.expert.pojo.ExtractResult;
import com.expert.pojo.Project;
import com.expert.pojo.User;
import com.expert.service.*;
import com.expert.vo.ExtractStatistic;
import com.expert.vo.HomeStatisticVo;
import com.expert.vo.ProjectStatistic;
import com.expert.vo.StatisticData;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.List;

/**
 * @ClassName: StatisticServiceImpl
 * @Author: cxs
 * @Date: 2024/03/29 19:09
 * @Description:
 **/
@Service
public class StatisticServiceImpl implements StatisticService {
    @Autowired
    UserService userService;
    @Autowired
    ExpertService expertService;

    @Autowired
    ProjectService projectService;
    @Autowired
    ExtractResultService extractService;
    /**
     * 用户统计
     *
     * @param num
     * @return
     */
    @Override
    public StatisticData userStatistic(Integer num) {
        List<String> dateList = getLocalDate(num);
        List<Long> totalNum=new ArrayList<>();
        List<Long> inc=new ArrayList<>();
        DateTimeFormatter format =DateTimeFormatter.ofPattern("yyyy-MM-dd");
        for (String s : dateList) {
            LocalDate dateTime=LocalDate.parse(s,format);
            LocalDateTime localDateTime=LocalDateTime.of(dateTime, LocalTime.MAX);
            Long total = this.userService.lambdaQuery().le(User::getCreateTime, localDateTime).count();
            totalNum.add(total);
        }
        StatisticData statisticData=new StatisticData();
        statisticData.setTimes(dateList);
        statisticData.setTotal(totalNum);
        //生成增长情况的情况

        //第一天
        LocalDate dateTime=LocalDate.parse(dateList.get(0),format);
        LocalDateTime localDateTime=LocalDateTime.of(dateTime, LocalTime.MAX);
        Long total = this.userService.lambdaQuery().le(User::getCreateTime, localDateTime).count();
        inc.add(total-totalNum.get(0));
        for (int i=1;i<totalNum.size();i++){
            inc.add(totalNum.get(i)- totalNum.get(i-1));
        }
        statisticData.setInc(inc);
        return statisticData;
    }

    /**
     * 专家统计
     *
     * @param num
     * @return
     */
    @Override
    public StatisticData expertStatistic(Integer num) {
        List<String> dateList = getLocalDate(num);
        List<Long> totalNum=new ArrayList<>();
        List<Long> inc=new ArrayList<>();
        DateTimeFormatter format =DateTimeFormatter.ofPattern("yyyy-MM-dd");
        for (String s : dateList) {
            LocalDate dateTime=LocalDate.parse(s,format);
            LocalDateTime localDateTime=LocalDateTime.of(dateTime, LocalTime.MAX);
            Long total = this.expertService.lambdaQuery().le(Expert::getCreateTime, localDateTime).count();
            totalNum.add(total);
        }
        StatisticData statisticData=new StatisticData();
        statisticData.setTimes(dateList);
        statisticData.setTotal(totalNum);
        //生成增长情况的情况
        //第一天
        LocalDate dateTime=LocalDate.parse(dateList.get(0),format);
        LocalDateTime localDateTime=LocalDateTime.of(dateTime, LocalTime.MAX);
        Long total = this.expertService.lambdaQuery().le(Expert::getCreateTime, localDateTime).count();
        inc.add(total-totalNum.get(0));
        for (int i=1;i<totalNum.size();i++){
            inc.add(totalNum.get(i)- totalNum.get(i-1));
        }
        statisticData.setInc(inc);
        return statisticData;
    }

    /**
     * 项目统计
     *
     * @param num
     * @return
     */
    @Override
    public ProjectStatistic projectStatistic(Integer num) {
        List<String> dateList = getLocalDate(num);
        List<Long> totalNum=new ArrayList<>();
        List<Long> overNum=new ArrayList<>();
        List<Long> inc=new ArrayList<>();
        DateTimeFormatter format =DateTimeFormatter.ofPattern("yyyy-MM-dd");
        for (String s : dateList) {
            LocalDate dateTime=LocalDate.parse(s,format);
            LocalDateTime localDateTime=LocalDateTime.of(dateTime, LocalTime.MAX);
            Long total = this.projectService.lambdaQuery().le(Project::getCreateTime, localDateTime).count();
            Long over=this.projectService.lambdaQuery()
                    .lt(Project::getProjectReviewTime,localDateTime)
                    .eq(Project::getProjectStatus, ProjectStatus.REVIEWED.getCode())
                    .count();
            overNum.add(over);
            totalNum.add(total);
        }
        //生成增长情况的情况
        //第一天
        LocalDate dateTime=LocalDate.parse(dateList.get(0),format);
        LocalDateTime localDateTime=LocalDateTime.of(dateTime, LocalTime.MAX);
        Long total =  this.projectService.lambdaQuery().le(Project::getCreateTime, localDateTime).count();
        inc.add(total-totalNum.get(0));
        for (int i=1;i<totalNum.size();i++){
            inc.add(totalNum.get(i)- totalNum.get(i-1));
        }
        ProjectStatistic statistic=new ProjectStatistic();
        statistic.setInc(inc);
        statistic.setTimes(dateList);
        statistic.setTotal(totalNum);
        statistic.setOver(overNum);
        return statistic;
    }

    /**
     * 评审结果统计
     *
     * @param num
     * @return
     */
    @Override
    public ExtractStatistic extractStatistic(Integer num) {
        List<String> dateList = getLocalDate(num);
        List<Long> yes=new ArrayList<>();
        List<Long> no=new ArrayList<>();
        List<Long> inc=new ArrayList<>();
        DateTimeFormatter format =DateTimeFormatter.ofPattern("yyyy-MM-dd");
        for (String s : dateList) {
            LocalDate dateTime=LocalDate.parse(s,format);
            LocalDateTime localDateTime=LocalDateTime.of(dateTime, LocalTime.MAX);
            Long yesNum = this.extractService.lambdaQuery().le(ExtractResult::getCreateTime, localDateTime).eq(ExtractResult::getObsolete,0).count();
            Long noNum=this.extractService.lambdaQuery()
                    .le(ExtractResult::getCreateTime, localDateTime)
                    .eq(ExtractResult::getObsolete,1)
                    .count();
            yes.add(yesNum);
            no.add(noNum);
        }
        //第一天
        LocalDate dateTime=LocalDate.parse(dateList.get(0),format);
        LocalDateTime localDateTime=LocalDateTime.of(dateTime, LocalTime.MAX);
        Long total =  this.extractService.lambdaQuery().le(ExtractResult::getCreateTime, localDateTime).count();
        inc.add(total-yes.get(0)-no.get(0));
        for (int i=1;i<dateList.size();i++){
            inc.add((yes.get(i)+no.get(i))- (yes.get(i-1)+no.get(i-1)));
        }
        ExtractStatistic extractStatistic=new ExtractStatistic();
        extractStatistic.setTimes(dateList);
        extractStatistic.setInc(inc);
        extractStatistic.setYes(yes);
        extractStatistic.setNo(no);
        return extractStatistic;
    }

    /**
     * 首页统计
     *
     * @return
     */
    @Override
    public HomeStatisticVo homeStatistic() {
        HomeStatisticVo homeStatisticVo=new HomeStatisticVo();
        homeStatisticVo.setExpertCount(this.expertService.count());
        homeStatisticVo.setUserCount(this.userService.count());
        homeStatisticVo.setProjectCount(this.projectService.count());
        return homeStatisticVo;
    }

    private List<String> getLocalDate(Integer num) {
        List<String> result = new ArrayList<>();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar today= Calendar.getInstance();//定义日期实例
        if (num == 1) {
            //获取最近七天
            while(result.size()<7){
                String date = format.format(today.getTime());
                result.add(date);
                today.set(Calendar.DATE,today.get(Calendar.DATE)-1);
            }
        } else if (num == 2) {
            //获取最近15天
            while(result.size()<15){
                String date = format.format(today.getTime());
                result.add(date);
                today.set(Calendar.DATE,today.get(Calendar.DATE)-1);
            }
        } else if (num == 3) {
            //获取最近三十天
            while(result.size()<30){
                String date = format.format(today.getTime());
                result.add(date);
                today.set(Calendar.DATE,today.get(Calendar.DATE)-1);
            }
        }
        Collections.reverse(result);
        return result;
    }
}
